//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.carhailing.system.consumer;

import com.carhailing.common.requst.DriverUserReq;
import com.carhailing.common.utils.MsgUtil;
import com.rabbitmq.client.Channel;
import java.io.IOException;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class SmsConsumer {
    private static final Logger log = LogManager.getLogger(SmsConsumer.class);
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private MsgUtil msgUtil;

    public SmsConsumer() {
    }

    @RabbitListener(
            queuesToDeclare = {@Queue("SEND_SMS")}
    )
    public void reciveSms(DriverUserReq userReq, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        String messageId = message.getMessageProperties().getMessageId();
        Long count = this.redisTemplate.opsForSet().add("SEND_SMS", new String[]{messageId});

        try {
            if (count != 1L) {
                channel.basicReject(deliveryTag, false);
            } else {
                log.info("发送短信，当前时间:{},消息内容:{}", (new Date()).toString(), userReq.getCode());
                this.msgUtil.sendMsg(userReq.getDriverPhone(), userReq.getCode());
                channel.basicAck(deliveryTag, false);
            }
        } catch (IOException var11) {
            this.redisTemplate.opsForSet().remove("SEND_SMS", new Object[]{messageId});

            try {
                channel.basicReject(deliveryTag, false);
            } catch (IOException var10) {
                throw new RuntimeException(var10);
            }

            throw new RuntimeException(var11);
        }
    }
}
